# crimes_dao.py
from sqlalchemy.orm import Session
from ..do.crime_do import CrimeModel


def getCrime(db: Session, crime_id: str):
    return db.query(CrimeModel).filter(CrimeModel.id == crime_id).first()

def getCrimes(db: Session, skip: int = 0, limit: int = 100):
    return db.query(CrimeModel).offset(skip).limit(limit).all()

def createCrime(db: Session, crime):
    crimeModel = CrimeModel(**crime)
    db.add(crimeModel)
    db.commit()
    db.refresh(crimeModel)
    return crimeModel

def updateCrime(db: Session, crime_id: str, crime):
    crimeModel = getCrime(db, crime_id)
    if crimeModel is None:
        return None
    for key, value in crime.items():
        setattr(crimeModel, key, value)
    db.commit()
    db.refresh(crimeModel)
    return crimeModel

def deleteCrime(db: Session, crime_id: str):
    crimeModel = getCrime(db, crime_id)
    if crimeModel is None:
        return False
    db.delete(crimeModel)
    db.commit()
    return True